/*
 * @(#)JSonResultTestCase.java $version 2010. 8. 11
 *
 * Copyright 2007-2010 NHN Corp. All rights Reserved. 
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nhncorp.usf.core.result;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;

import com.opensymphony.xwork.ActionContext;

import com.nhncorp.lucy.common.config.model.ApplicationInfo;
import com.nhncorp.lucy.web.helper.ServletHelper;
import com.nhncorp.usf.core.config.runtime.ResultPageInfo;
import com.nhncorp.usf.core.config.runtime.ResultPageInfo.DISPATCHER_TYPE;
import com.nhncorp.usf.core.result.direct.HtmlResult;
import com.nhncorp.usf.core.result.direct.JSonResult;
import junit.framework.Assert;

/**
 * {@link HtmlResult} TestCase.
 * 
 * @author Web Platform Development Team
 */
public class JSonResultTestCase {
    AbstractResult directResult = new JSonResult();
    ResultPageInfo resultPageInfo;

    public static void initialize() throws Exception {
        MockHttpServletRequest request = new MockHttpServletRequest();
        MockHttpServletResponse response = new MockHttpServletResponse();
        ActionContext.getContext().put(ServletHelper.HTTP_REQUEST, request);
        ActionContext.getContext().put(ServletHelper.HTTP_RESPONSE, response);

        ApplicationInfo.setPageEncoding("utf-8");
    }

    @Test
    public void executeScriptResult_Map() throws Exception {
        initialize();

        resultPageInfo = new ResultPageInfo(DISPATCHER_TYPE.direct, null);
        resultPageInfo.setId("dummy_id");
        resultPageInfo.getProperties().setProperty("contentType", "text/html");
        resultPageInfo.getProperties().setProperty("language", "json");
        resultPageInfo.getProperties().setProperty("src", "result");

        BeanUtils.populate(directResult, resultPageInfo.getProperties());

        MockHttpServletResponse response = (MockHttpServletResponse) ServletHelper
                .getResponse();
        try {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            Map<String, Object> resultMap = new HashMap<String, Object>();
            resultMap.put("test", "hahaha!");
            dataMap.put("result", resultMap);

            directResult.execute(resultPageInfo, dataMap);
            String contents = response.getContentAsString().trim();

            Assert.assertEquals("{\"test\":\"hahaha!\"}", contents);
        } catch (Exception except) {
            Assert.fail();
        }
    }

    @Test
    public void executeScriptResult_List() throws Exception {
        initialize();

        resultPageInfo = new ResultPageInfo(DISPATCHER_TYPE.direct, null);
        resultPageInfo.setId("dummy_id");
        resultPageInfo.getProperties().setProperty("contentType", "text/html");
        resultPageInfo.getProperties().setProperty("language", "json");
        resultPageInfo.getProperties().setProperty("src", "result");

        BeanUtils.populate(directResult, resultPageInfo.getProperties());

        MockHttpServletResponse response = (MockHttpServletResponse) ServletHelper
                .getResponse();
        try {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            List<String> resultList = new ArrayList<String>();
            resultList.add("test");
            resultList.add("hahaha");
            dataMap.put("result", resultList);

            directResult.execute(resultPageInfo, dataMap);
            String contents = response.getContentAsString().trim();

            Assert.assertEquals("[\"test\",\"hahaha\"]", contents);
        } catch (Exception except) {
            Assert.fail();
        }
    }

    @Test
    public void executeScriptResult_Array() throws Exception {
        initialize();

        resultPageInfo = new ResultPageInfo(DISPATCHER_TYPE.direct, null);
        resultPageInfo.setId("dummy_id");
        resultPageInfo.getProperties().setProperty("contentType", "text/html");
        resultPageInfo.getProperties().setProperty("language", "json");
        resultPageInfo.getProperties().setProperty("src", "result");

        BeanUtils.populate(directResult, resultPageInfo.getProperties());

        MockHttpServletResponse response = (MockHttpServletResponse) ServletHelper
                .getResponse();
        try {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            String[] resultArr = { "abc", "def" };

            dataMap.put("result", resultArr);

            directResult.execute(resultPageInfo, dataMap);
            String contents = response.getContentAsString().trim();

            Assert.assertEquals("[\"abc\",\"def\"]", contents);
        } catch (Exception except) {
            Assert.fail();
        }
    }

    @Test
    public void executeMultiJsonObject() throws Exception {
        initialize();

        resultPageInfo = new ResultPageInfo(DISPATCHER_TYPE.direct, null);
        resultPageInfo.setId("dummy_id");
        resultPageInfo.getProperties().setProperty("contentType", "text/html");
        resultPageInfo.getProperties().setProperty("language", "json");
        resultPageInfo.getProperties()
                .setProperty("src", "profile,result,info");

        BeanUtils.populate(directResult, resultPageInfo.getProperties());

        MockHttpServletResponse response = (MockHttpServletResponse) ServletHelper
                .getResponse();
        try {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            Map<String, Object> inputMap1 = new HashMap<String, Object>();
            inputMap1.put("test", "hahaha!");
            inputMap1.put("name", "choco");
            dataMap.put("profile", inputMap1);
            String[] resultArr = { "abc", "def" };
            dataMap.put("result", resultArr);

            Map<String, Object> inputMap2 = new HashMap<String, Object>();
            inputMap2.put("company", "nhn");
            dataMap.put("info", inputMap2);

            directResult.execute(resultPageInfo, dataMap);
            String contents = response.getContentAsString().trim();

            Assert.assertEquals("{" + "\"result\":[" + "\"abc\"," + "\"def\""
                    + "]," + "\"info\":{\"company\":\"nhn\"},"
                    + "\"profile\":{" + "\"test\":\"hahaha!\","
                    + "\"name\":\"choco\"" + "}" + "}", contents);
        } catch (Exception except) {
            Assert.fail();
        }
    }

}
